﻿using S.Archives.Domain.S.ArchivesDbContext;
using S.Archives.Domain.S.ArchivesDto;
using S.Archives.Domain.S.ArchivesModel;
using S.Archives.Infrstructre.InterFace;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace S.Archives.Infrstructre.Imp
{
    public class SheepFileRepository : BaseRepository<SheepFileModel>, ISheepFileRepository
        
    {
        private ArchivesDbContext DbContext { get; set; }
        private readonly DbHelper dbHelper;
        public SheepFileRepository(ArchivesDbContext db, DbHelper dbHelper) : base(db)
        {
            this.dbHelper = dbHelper;
        }
        /// <summary>
        /// 获取档案信息
        /// </summary>
        /// <returns></returns>
        public async Task<List<SheepFileDto>> GetSheepFiles()
        {
            string sql = "SELECT DISTINCT DATEDIFF(day, ReproductiveStateDate, GETDATE()) as StateDays, pr.[Currents] as ProductionRank,  er.EITag, sf.*,la.WeaningWeight,la.WeaningDate, datediff(day,sf.BirthDate,la.WeaningDate) as WeaningAge, CASE WHEN pt.EarTag IS NOT NULL THEN 1 ELSE 0 END AS IsPregnancy, b.SurrenderDate, c.BuildingName, d.ColumnName, DATEDIFF(MONTH, sf.BirthDate, GETDATE()) + CASE WHEN DATEPART(DAY, sf.BirthDate) > DATEPART(DAY, GETDATE()) THEN -1 ELSE 0 END AS MonthAge, case when DATEDIFF(MONTH, sf.BirthDate, GETDATE()) + CASE WHEN DATEPART(DAY, sf.BirthDate) > DATEPART(DAY, GETDATE()) THEN -1 ELSE 0 END>=0 and DATEDIFF(MONTH, sf.BirthDate, GETDATE()) + CASE WHEN DATEPART(DAY, sf.BirthDate) > DATEPART(DAY, GETDATE()) THEN -1 ELSE 0 END<=3 then 1 when DATEDIFF(MONTH, sf.BirthDate, GETDATE()) + CASE WHEN DATEPART(DAY, sf.BirthDate) > DATEPART(DAY, GETDATE()) THEN -1 ELSE 0 END>3 and DATEDIFF(MONTH, sf.BirthDate, GETDATE()) + CASE WHEN DATEPART(DAY, sf.BirthDate) > DATEPART(DAY, GETDATE()) THEN -1 ELSE 0 END<=6 then 2 else 3 end as GrowthStage,  DATEDIFF(DAY, sf.BirthDate, GETDATE()) AS Dayage, DATEDIFF(DAY, sf.BirthDate, la.WeaningDate) AS WeaningAge FROM SheepFileModel sf LEFT JOIN MaternalMating mm ON sf.EarTag = mm.EarTag  LEFT JOIN MaternalWeaning mw ON sf.EarTag = mw.EarTag LEFT JOIN LambAblactation la ON sf.EarTag = la.EarTag LEFT JOIN BodyMeasureModel bm ON sf.EarTag = bm.EarTag LEFT JOIN AppearanceModel am ON sf.EarTag = am.EarTag LEFT JOIN DeathModel dm ON sf.EarTag = dm.EarTag LEFT JOIN SheepRescission sr ON sf.EarTag = sr.EarTag LEFT JOIN BreedingHistoryModel bh ON sf.EarTag = bh.EarTag LEFT JOIN PregnancytestModel pt ON sf.EarTag = pt.EarTag LEFT JOIN ParturitionModel p ON sf.EarTag = p.EarTag LEFT JOIN SheepSaleModels ss ON sf.EarTag = ss.EarTag LEFT JOIN EliminationModel em ON sf.EarTag = em.EarTag LEFT JOIN DescendantTransfer dt ON sf.EarTag = dt.EarTag LEFT JOIN MarkModel er ON sf.EarTag = er.EarTag LEFT JOIN EarTagOrMarkModel eom ON er.MarkId = eom.MarkId LEFT JOIN EarTagModel etm ON eom.EarTagId = etm.EarTagId left join SheepRescission b on sf.EarTag =b.EarTag left join ColumnManagementModel c on b.ShiftToBuild=c.BuildingManagementId left join FieldModel d on b.ShiftToHuts=d.ColumnManagementId left join ProductionGrading pr on sf.EarTag=pr.RankEarTag";
            var list = await dbHelper.GetListDataBySql<SheepFileDto>(sql);
            var result = list.GroupBy(x => x.EarTag).Select(g => g.OrderByDescending(x => x.SurrenderDate).First()).ToList();


            return result;
        }
    }
}
